/*
 *所有关于Ware_OutWareHouseBillList类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Ware_OutWareHouseBillListService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using iMES.Core.BaseProvider;
using iMES.Core.Extensions;
using iMES.Core.Extensions.AutofacManager;
using iMES.Core.Utilities;
using iMES.Entity.DomainModels;
using iMES.Warehouse.IRepositories;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace iMES.Warehouse.Services
{
    public partial class Ware_OutWareHouseBillListService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IWare_OutWareHouseBillListRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public Ware_OutWareHouseBillListService(
            IWare_OutWareHouseBillListRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override PageGridData<Ware_OutWareHouseBillList> GetPageData(PageDataOptions options)
        {
            // 构建缓存Key，建议包含分页和查询条件
            string cacheKey = $"Ware_OutWareHouseBillList:Page:{options.Page}:Rows:{options.Rows}:Wheres:{options.Wheres ?? ""}:Sort:{options.Sort ?? ""}:Order:{options.Order ?? ""}";

            // 获取Redis连接（假设已通过依赖注入注册IConnectionMultiplexer）
            var redis = AutofacContainerModule.GetService<IConnectionMultiplexer>();
            var db = redis.GetDatabase();

            // 先查缓存
            string cacheValue = db.StringGet(cacheKey);
            if (!string.IsNullOrEmpty(cacheValue))
            {
                return JsonConvert.DeserializeObject<PageGridData<Ware_OutWareHouseBillList>>(cacheValue);
            }

            // 缓存没有，查数据库
            var result = base.GetPageData(options);

            // 写入缓存，设置过期时间（如10分钟）
            db.StringSet(cacheKey, JsonConvert.SerializeObject(result), TimeSpan.FromMinutes(10));

            return result;
        }

    }
}
